package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysBuildingMapper;
import com.ruoyi.system.domain.SysBuilding;
import com.ruoyi.system.service.ISysBuildingService;

/**
 * 楼Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-07
 */
@Service
public class SysBuildingServiceImpl implements ISysBuildingService 
{
    @Autowired
    private SysBuildingMapper sysBuildingMapper;

    @Autowired
    private RedisCache redisCache;
    /**
     * 查询楼
     * 
     * @param buildingId 楼主键
     * @return 楼
     */
    @Override
    public SysBuilding selectSysBuildingByBuildingId(Long buildingId)
    {
        return sysBuildingMapper.selectSysBuildingByBuildingId(buildingId,null,null);
    }

    /**
     * 查询楼列表
     * 
     * @param sysBuilding 楼
     * @return 楼
     */
    @Override
    public List<SysBuilding> selectSysBuildingList(SysBuilding sysBuilding)
    {
        return sysBuildingMapper.selectSysBuildingList(sysBuilding);
    }


    /**
     * 查询所有楼名列表
     *
     * @param sysBuilding 楼
     * @return 楼集合
     */
    @Override
    public List<SysBuilding> selectAllSysBuildingList(SysBuilding sysBuilding){
        return sysBuildingMapper.selectAllSysBuildingList(sysBuilding);
    }

    /**
     * 查询总热量
     * @param deptId
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public BigDecimal selectTotalHeatQuantity( Long deptId, Date beginDate, Date endDate ){
        return sysBuildingMapper.selectTotalHeatQuantity(deptId,beginDate,endDate);
    }
    /**
     * 新增楼
     * 
     * @param sysBuilding 楼
     * @return 结果
     */
    @Override
    public int insertSysBuilding(SysBuilding sysBuilding)
    {
        sysBuilding.setCreateTime(DateUtils.getNowDate());
        if (sysBuilding.getPriceType().equals(UserConstants.BUILDING_PRICE_TYPE_USER)){
            sysBuilding.setPrice(UserConstants.BUILDING_PRICE_USER);
            sysBuilding.setHeatPrice(UserConstants.BUILDING_HEAT_PRICE_USER);
        } else {
            sysBuilding.setPrice(UserConstants.BUILDING_PRICE_UN_USER);
            sysBuilding.setHeatPrice(UserConstants.BUILDING_HEAT_PRICE_UN_USER);
        }
        return sysBuildingMapper.insertSysBuilding(sysBuilding);
    }

    /**
     * 批量新增楼
     *
     * @param sysBuildingList 楼
     * @return 结果
     */
    @Override
    public void bathInsertSysBuilding(List<SysBuilding> sysBuildingList, Integer floorCount, Long deptId){
        for (int i = 0; i < floorCount; i++){
            for (SysBuilding sysBuilding : sysBuildingList){
                sysBuilding.setFloor(i + 1);
                sysBuilding.setDeptId(deptId);
                sysBuildingMapper.insertSysBuilding(sysBuilding);
            }
        }
        Set<String> bathchSet = redisCache.getCacheSet(CacheConstants.BATCH_BUILDING + sysBuildingList.get(0).getBatchKey(deptId));
        bathchSet.remove(sysBuildingList.get(0).getBuildingNo());
        redisCache.setCacheSet(CacheConstants.BATCH_BUILDING + sysBuildingList.get(0).getBatchKey(deptId), bathchSet);
    }
    /**
     * 修改楼
     * 
     * @param sysBuilding 楼
     * @return 结果
     */
    @Override
    public int updateSysBuilding(SysBuilding sysBuilding)
    {
        if (sysBuilding.getPriceType().equals(UserConstants.BUILDING_PRICE_TYPE_USER)){
            sysBuilding.setPrice(UserConstants.BUILDING_PRICE_USER);
            sysBuilding.setHeatPrice(UserConstants.BUILDING_HEAT_PRICE_USER);
        } else {
            sysBuilding.setPrice(UserConstants.BUILDING_PRICE_UN_USER);
            sysBuilding.setHeatPrice(UserConstants.BUILDING_HEAT_PRICE_UN_USER);
        }
        sysBuilding.setUpdateTime(DateUtils.getNowDate());
        return sysBuildingMapper.updateSysBuilding(sysBuilding);
    }

    /**
     * 批量删除户
     * 
     * @param buildingIds 需要删除的楼主键
     * @return 结果
     */
    @Override
    public int deleteSysBuildingByBuildingIds(Long[] buildingIds)
    {
        return sysBuildingMapper.deleteSysBuildingByBuildingIds(buildingIds);
    }

    /**
     * 删除户信息
     * 
     * @param buildingId 楼主键
     * @return 结果
     */
    @Override
    public int deleteSysBuildingByBuildingId(Long buildingId)
    {
        return sysBuildingMapper.deleteSysBuildingByBuildingId(buildingId);
    }
    /**
     * 删除楼信息
     *
     * @return 结果
     */
    @Override
    public int deleteSysBuildingByBuildingNo(String buildingNo){
        return sysBuildingMapper.deleteSysBuildingByBuildingNo(buildingNo);
    }

    /**
     * 绑定热费
     * @param housingId
     * @param heatFeeId
     */
    @Override
    public void bindHeatFeedId( Long heatFeeId,Long housingId){
        sysBuildingMapper.bindHeatFeedId(heatFeeId,housingId);
    }

    /**
     * 解绑热费
     * @param heatFeeId
     */
    @Override
    public void restHeatFeedId(Long heatFeeId){
        sysBuildingMapper.restHeatFeedId(heatFeeId);
    }
}
